﻿using lingopen.Application;
using lingopen.Application.Jabil;
using lingopen.Entity.CNC;
using Microsoft.AspNetCore.Mvc;

namespace lingopen.Jabil.Server.Areas.BLE.Controllers
{
    /// <summary>
    /// CNC管理
    /// </summary>
    [Area(AreaNames.CNC)]
    [LoginAuthorize]
    public class TransferController : WebController
    {
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pagination">页码</param>
        /// <param name="keyword">关键字</param>
        /// <param name="type">link5 cnc5-1 cnc5-2 cnc6 lin7</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetModels(Pagination pagination, string keyword, string type)
        {
            PagedData<CNC_Transfer> data = this.CreateService<IEPCAppService>().Query_CNC_Transfers(pagination, keyword, type);
            return this.UISuccessData(data);
        }


        [HttpPost]
        [SkipLoginAuthorizeAttribute]
        //[ValidateAntiForgeryToken]
        public ActionResult Pass([FromBody]Add_CNC_Transfer input, string type)
        {
            if (input.Prod_Code.IsNull()) return UIFailedData(input.Request_ID, "产品码不能为空");
            if (input.Tool_Code.IsNull()) return UIFailedData(input.Request_ID, "治具码不能为空");
            input.CreateUserId = this.CurrentSession == null ? "9f2ec0797d0f4fe290ab8b09a8302aba" : this.CurrentSession.UserId;
            input.Station_Type = type;
            try
            {
                bool check_repeate = this.CreateService<IEPCAppService>().Pass_Repeate(input.Prod_Code, input.Tool_Code, type);
                if (!check_repeate)
                {
                    return UIFailedData(input.Request_ID, $"重复过站");
                }
                var add = this.CreateService<IEntityAppService>().AddFromDto<CNC_Transfer, Add_CNC_Transfer>(input);
                return this.UISuccessData(add.Request_ID);
            }
            catch (System.Exception ex)
            {
                return UIFailedData(input.Request_ID, ex.Message);
            }

        }
        [HttpPost]
        [SkipLoginAuthorizeAttribute]
        public ActionResult Check([FromBody]Add_CNC_Check input, string type)
        {
            if (input.Prod_Code.IsNull()) return UIFailedData(input.Request_ID, "产品码不能为空");
            if (input.Tool_Code.IsNull()) return UIFailedData(input.Request_ID, "治具码不能为空");
            input.Station_Type = type;
            try
            {
                //var check_link = this.CreateService<IEPCAppService>().Check_Link(input.Tool_Code, input.Prod_Code, type);
                //if (!check_link)
                //    return UIFailedData(input.Request_ID, $"未Link");
                bool check_repeate = this.CreateService<IEPCAppService>().Pass_Repeate(input.Prod_Code, input.Tool_Code, type);
                if (!check_repeate)
                {
                    return UIFailedData(input.Request_ID, $"重复加工");
                }
                //var check_repeate = this.CreateService<IEPCAppService>().Check_Repeate(input.Prod_Code, input.Tool_Code, type);
                //if (!check_repeate)
                //    return UIFailedData(input.Request_ID, $"重复加工");
                var check_flow = this.CreateService<IEPCAppService>().Check_Flow(input.Prod_Code, input.Tool_Code, type);
                if (!check_flow)
                    return UIFailedData(input.Request_ID, $"工站不匹配");

               
                var add = this.CreateService<IEntityAppService>().AddFromDto<CNC_Check, Add_CNC_Check>(input);
                return this.UISuccessData(add.Request_ID); 

            }
            catch (System.Exception ex)
            {
                return UIFailedMsg(ex.Message);
            }
        }
        [HttpPost]
        //[ValidateAntiForgeryToken]
        public ActionResult Update(Update_CNC_Transfer input)
        {
            this.CreateService<IEntityAppService>().UpdateFromDto<CNC_Transfer, Update_CNC_Transfer>(input);
            return this.UISuccessData();
        }

        [HttpPost]
        //[ValidateAntiForgeryToken]
        public ActionResult Delete(string id)
        {
            this.CreateService<IEntityAppService>().SoftDelete<CNC_Transfer>(id, this.CurrentSession.UserId);
            return this.UISuccessData();
        }




    }
}
